package com.yuyou.fn.controller;

import com.google.common.collect.Maps;
import com.yuyou.fn.educational.entity.BaseGrade;
import com.yuyou.fn.educational.entity.BaseSubject;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.entity.BusTeam;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.platform.service.*;
import com.yuyou.fn.platform.vo.SysResourceVo;
import com.yuyou.fn.vo.SysUserLoginSuccessVo;
import com.yuyou.fn.vo.UserConfigVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * Created by wens on 2017/2/22.
 */
@Controller
@RequestMapping("/api/admin")
public class AdminCommonApiController extends AdminApiBaseController {

    @Resource
    private ICaptchaService captchaService;

    @Resource
    private IAccessTokenService accessTokenService;

    @Resource
    private IBusTeamService busTeamService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private ISysResourceService sysResourceService;

    @Resource
    private IBaseGradeService baseGradeService;

    @Resource
    private IBaseSubjectService baseSubjectService;

    @Resource
    private ICourseTemplateService iCourseTemplateService;

    @Resource
    private ITeacherService iTeacherService;

    @Resource
    private IPeriodService iPeriodService;

    @Resource
    private IRoomService iRoomService;

    @Resource
    private ICampusService iCampusService;


    @ApiOperation("登陆接口")
    @RequestMapping(value = "login", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<SysUserLoginSuccessVo> login(@RequestParam(value = "username") String username, @RequestParam("password") String password, @RequestParam(value = "captchaCode", required = false) String captchaCode, @RequestParam(value = "u", required = false) String uid, HttpSession session) {

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return ReturnResult.fail("用户名和密码不能为空");
        }

        SysUser user = sysUserService.login(username, password);

        if (user == null) {
            Long failTime = captchaService.opsStat(username, "login");
            HashMap<String, Object> ret = Maps.newHashMap();
            ret.put("failTime", failTime);
            return ReturnResult.fail("用户名或密码不正确", ret);
        }


        if (user.getStatus() == 0) {
            return ReturnResult.fail("你的帐号被禁用，暂时不能登录");
        }

        if (StringUtils.isEmpty(uid)) {
            uid = session.getId();
        }

        if (captchaService.needVerifyCode(username, "login", 3)) {
            captchaService.verifyCode(uid, captchaCode, "login");
        }

        String accessToken = accessTokenService.generateToken(user.getUserId());
        return ReturnResult.success(new SysUserLoginSuccessVo(accessToken, user));
    }


    @ApiOperation("检查登陆是否需要验证码")
    @RequestMapping(value = "/checkNeedCaptcha", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Boolean> checkNeedCaptcha(@RequestParam("username") String username) {
        Boolean ret = null;
        if (captchaService.needVerifyCode(username, "login", 3)) {
            ret = Boolean.TRUE;
        } else {
            ret = Boolean.FALSE;
        }
        return ReturnResult.success(ret);
    }

    @ApiOperation("修改自己密码")
    @RequestMapping(value = "/changeMyPassword", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> changeMyPassword(@RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword, HttpServletRequest request) {
        sysUserService.changePassword(getUserId(request), oldPassword, newPassword);
        return ReturnResult.success("OK");
    }


    @ApiOperation("获取配置(权限)")
    @RequestMapping(value = "/config", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<UserConfigVo> config(HttpServletRequest request) {

        SysUser user = getUser(request);

        List<SysResourceVo> resourceList = sysResourceService.findResourceByUserIdFromCache(user.getUserId());
        List<BusTeam> busTeamList = busTeamService.findBusTeamForList(user.getAreaTeamId(),user.getBusTeamId());
        List<AreaTeam> areaTeamList = areaTeamService.findAreaTeamForList(user.getAreaTeamId());
        List<BaseGrade> gradeList = baseGradeService.findAllFromCache();
        List<BaseSubject> subjectList = baseSubjectService.findAllFromCache();
        List<Period> periodList = iPeriodService.findAllFromCache();

        UserConfigVo userConfigVo = new UserConfigVo();
        userConfigVo.setUserType(user.getUserType());
        userConfigVo.setBusTeamList(busTeamList);
        userConfigVo.setAreaTeamList(areaTeamList);
        userConfigVo.setResourceList(resourceList);
        userConfigVo.setGradeList(gradeList);
        userConfigVo.setSubjectList(subjectList);
        userConfigVo.setPeriodList(periodList);
        return ReturnResult.success(userConfigVo);
    }
}
